﻿namespace FallingRocks  //TODO: Exeption IsAConsoleKey
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;

    public class Controls
    {
        static public Keys key = new Keys();

        public Controls()               //Import the last Settings
        {
            string file = "../../settings.ini";
            ReloadSettings(file);
        }

        public void DefaultSettings()   //Set the Default Settings
        {
            ReloadSettings("../../defaultSettings.ini");
        }

        private void ReloadSettings(string file)
        {
            string[] lines = ReadFile_Controls(file);

            string[] command = {"MOVING_LEFT",
                                "MOVING_RIGHT",
                                "ACTION",
                                "PAUSE"};

            foreach (string line in lines)
            {
                var fragments = Regex.Match(line, "(.*?) = (.*)").Groups;

                for (int i = 0; i < command.Length; i++)
                    if (fragments[1].Value == command[i])
                    {
                        string strKeys = fragments[2].ToString();

                        System.Windows.Forms.SendKeys.SendWait(strKeys.Trim());
                        ConsoleKey keys = Console.ReadKey().Key;

                        switch (i)
                        {
                            case 0:
                                key.MovingLeft = keys;
                                break;
                            case 1:
                                key.MovingRight = keys;
                                break;
                            case 2:
                                key.Activate = keys;
                                break;
                            case 3:
                                key.Pause = keys;
                                break;
                            default:
                                break;
                        }
                    }
            }
        }

        static public void SetNewSettings()     //TODO
        {
            /*
            Press Key For movingLeft
            key.MovingLeft = Console.ReadKey().Key; 
            
            Press Key For movingRight
            key.MovingRight = keys;
            
            Press Key For Activate
            key.Activate = keys;
            
            Press Key For Pause
            key.Pause = keys;
            
            ExportSettings();
            */
        }

        static public void ExportSettings()     //export the new Settings
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[SETTINGS]\n");
            sb.Append("MOVING_LEFT = " + key.MovingLeft+'\n');
            sb.Append("MOVING_RIGHT = " + key.MovingRight + '\n');
            sb.Append("ACTION = " + key.Activate + '\n');
            sb.Append("PAUSE = " + key.Pause + '\n');                  //TODO add background color or something else
            WriteFile_Controls(sb.ToString().Split('\n'));
        }

        static void WriteFile_Controls(string[] lines)
        {
            using (StreamWriter output = new StreamWriter("../../settings.ini"))
                foreach (string line in lines)
                    output.WriteLine(line);
        }

        static private string[] ReadFile_Controls(string file)
        {
            using (StreamReader input = new StreamReader("../../settings.ini"))
            {
                List<string> list = new List<string>();

                for (string line; (line = input.ReadLine()) != null; )
                    if (line.Contains('='))
                        list.Add(line);

                string[] lines = new string[list.Count];

                for (int i = 0; i < lines.Length; i++)
                    lines[i] = list[i].ToString();

                return lines;
            }
        }
    }
}
